package org.jbit.news.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.eclipse.jdt.internal.compiler.ast.ThisReference;
import org.jbit.news.biz.CommentBiz;
import org.jbit.news.dao.BaseDao;
import org.jbit.news.dao.CommentsDao;
import org.jbit.news.entity.Comment;

public class CommentsDaoImpl extends BaseDao implements CommentsDao {

	public List<Comment> geComments(String nid) {
		ResultSet rs=null;
		String sql="select * from comments where cnid=?";
		 rs=this.exxcuteQuery(sql, nid);
		 List<Comment> lists =new ArrayList<Comment>();
		 try {
			while (rs.next()) {
				Comment comment=new Comment();
				comment.setCnid(rs.getString("cnid"));
				comment.setCcontent(String.valueOf(rs.getObject("ccontent")));
				comment.setCdate(String.valueOf(rs.getObject("cdate")));
				comment.setCip(String.valueOf(rs.getObject("cip")));
				comment.setCauthor(String.valueOf(rs.getObject("cauthor")));
				lists.add(comment);
				
				
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			this.closeAll(connection, pstmt, null);
		}
		
		return lists;
	}

	public int save(Comment comment) {
		String sql="insert into comments(cnid,ccontent,cdate,cip,cauthor) values(?,?,?,?,?)";
		Object[] params={ comment.getCnid(),comment.getCcontent(),comment.getCdate(),comment.getCip(),comment.getCauthor()};
		int num=this.executeUpdate(sql, params);
		return num;
	}
//public static void main(String[] args) {
//	Comment comment=new Comment();
//	CommentsDaoImpl commentsDaoImpl=new CommentsDaoImpl();
//	
////	comment.setCnid("108");
//	comment.setCcontent("23"); 
//	comment.setCdate("1234-12-12");
//	comment.setCip("123");
//	comment.setCauthor("45");
//	int num=commentsDaoImpl.save(comment);
//}
	
	public static void main(String[] args) {
		Comment cc=new Comment();
//	,comment.getCauthor()
		cc.setCnid("108");
		cc.setCcontent("1");
		cc.setCdate("2017-10-13 14:24:02");
		cc.setCip("1");
		cc.setCauthor("zz");
		CommentsDao cc1=new CommentsDaoImpl();
		int a =cc1.save(cc);
		if(a>0){
			System.out.println(1);
		}else {
			System.out.println(2);
		}
	}
}
